load("@fbcode//antlir/fbpkg:fbpkg.bzl", "fbpkg")
load("@fbcode_macros//build_defs:cpp_binary.bzl", "cpp_binary")
load("@fbcode_macros//build_defs:export_files.bzl", "export_file")

oncall("fbcode_entropy_wardens_folly")

cpp_binary(
    name = "folly-addr2line",
    srcs = [
        "Addr2Line.cpp",
    ],
    deps = [
        "//folly:range",
        "//folly/experimental/symbolizer:dwarf",
        "//folly/experimental/symbolizer:elf",
        "//folly/experimental/symbolizer:symbolized_frame",
        "//folly/experimental/symbolizer:symbolizer",
        "//folly/init:init",
        "//folly/portability:gflags",
    ],
)

export_file(
    name = "libFollySegFault.so.v",
)

# Build a standlone DSO for use w/ `LD_PRELOAD`.
cpp_binary(
    name = "libFollySegFault.so",
    srcs = [
        "LibSegFault.cpp",
    ],
    compiler_flags = [
        # Allow using the elvis operator.
        "-Wno-gnu-conditional-omitted-operand",
    ],
    dlopen_enabled = True,
    linker_flags = [

        # Trim runtime `DT_NEEDED` deps.
        "--as-needed",

        # Use a version script to prevent exporting any symbols.
        "--version-script=$(location :libFollySegFault.so.v)",
    ] + select({
        "DEFAULT": [],
        "ovr_config//runtime:fbcode": [
            # This how we can statically link `libstdc++.so` w/ the fbcode
            # toolchain.
            "--push-state",
            "-Bstatic",
            "-lstdc++_pic",
            "--pop-state",
        ],
    }),
    # Statically link libstdc++ to avoid an external dep which could make
    # this less flexible.
    os_linker_flags = [
        (
            "linux",
            select({
                "DEFAULT": [
                    "-static-libstdc++",
                ],
                "ovr_config//runtime:fbcode": [],
            }),
        ),
    ],
    deps = [
        "//folly:range",
        "//folly/experimental/symbolizer:signal_handler",
    ],
)

fbpkg.builder(
    name = "folly.symbolizer",
    buck_opts = fbpkg.buck_opts(
        config = {"fbcode.platform": "centos8"},
    ),
    path_actions = {
        "folly-addr2line": ":folly-addr2line",
        "libFollySegFault.so": ":libFollySegFault.so",
    },
)
